Back to course home
0% completed
Vote For New Content
Meta Hard Problems
Sliding Window Median (hard)
Solution: Sliding Window Median
Alien Dictionary (hard)
Solution: Alien Dictionary
Minimum Window Substring (hard)
Solution: Minimum Window Substring
Best Meeting Point (hard)
Solution: Best Meeting Point
Maximum Sum of 3 Non-Overlapping Subarrays (hard)
Solution: Maximum Sum of 3 Non-Overlapping Subarrays
Making A Large Island (hard)
Solution: Making A Large Island
Basic Calculator (Hard)
Solution: Basic Calculator
Serialize and Deserialize Binary Tree (hard)
Solution: Serialize and Deserialize Binary Tree
Longest Increasing Path in a Matrix (hard)
Solution: Longest Increasing Path in a Matrix
Valid Palindrome III (hard)
Solution: Valid Palindrome III
Clone Graph (medium)
Problem Statement
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.
Example 1:
Input:
1--2
| |
4--3
Expected Output:
1--2
| |
4--3
Explanation: The graph has four nodes with the following connections:
- Node
1is connected to nodes2and4. - Node
2is connected to nodes1and3. - Node
3is connected to nodes2and4. - Node
4is connected to nodes1and3.
Example 2:
Input:
1--2
/ \
5 3
|
4
Expected Output:
1--2
/ \
5 3
|
4
Explanation: The graph consists of five nodes with these connections:
- Node
1is connected to nodes2and5. - Node
2is connected to nodes1and3. - Node
3is connected to nodes2and4. - Node
4is connected to node3. - Node
5is connected to node1.
Example 3:
Input:
1--2
/ \
4 3
\ /
5--6
Expected Output:
1--2
/ \
4 3
\ /
5--6
Explanation: The graph has six nodes with the following connections:
- Node
1is connected to nodes2and4. - Node
2is connected to nodes1and3. - Node
3is connected to nodes2and6. - Node
4is connected to nodes1and5. - Node
5is connected to nodes4and6. - Node
6is connected to nodes3and5.
Constraints:
- The number of nodes in the graph is in the range
[0, 100]. 1 <= Node.val <= 100Node.valis unique for each node.- There are no repeated edges and no self-loops in the graph.
- The Graph is connected and all nodes can be visited starting from the given node.
Try it yourself
Try solving this question here:
Java
Saved to Cloud
. . . .
Solution: Number of Islands
Solution: Clone Graph
Mark as Completed
On this page
Problem Statement
Try it yourself